#include "ms618_algside1datum.h"

using namespace MeasurE618;
AlgSide1Datum::AlgSide1Datum(int i):AlgBase(i)
{

}

void AlgSide1Datum::operator()(cv::Mat &srcImg, int id)
{
    //side1 图   用GetSideDatum616（SetUp4-x结果，SetUp4-y结果,输出SetUp-4基准点）
    //int GetLineContours(cv::Mat srcimg, const vector<int> paras, vector<cv::Point>& contours);

    std::vector<std::vector<cv::Point>> resptVec;
    std::vector<std::vector<cv::Rect>> rangVec2;
    for(auto i=35;i<37;i++)
    {
        std::vector<int> tparas;
        for(auto j=0;j< m_dataAllVec[i].m_edgeParam.cols;j++)
        {
            tparas.push_back(m_dataAllVec[i].m_edgeParam.at<int>(i-35,j));
        }
        std::vector<cv::Rect> rangVec;
        TemplateMatch(srcImg,m_dataAllVec[i],rangVec);
        rangVec2.push_back(rangVec);
        cv::Mat imgt=srcImg(rangVec[0]).clone();
        std::vector<cv::Point> contours;
        edgedetect obj;
        obj.GetLineContours(imgt, tparas, contours);
        ImageCoordinateTransformation(contours, rangVec);

#ifdef MEASUR_E618_DRAW_IMG
        cv::Mat srcImgTemp = srcImg.clone();
        static int sideDatumIndex = 0;
        std::string fileName = "sideDatum_" + to_string(sideDatumIndex);
        fileName += ".jpg";

        for(int i = 0; i < contours.size(); ++i) {
            std::cout << "contours x: " << contours[i].x
                      << "contours y: " << contours[i].y
                      << std::endl;

            cv::circle(srcImg, contours[i], 20, 160, -1);
        }
        imwrite(fileName, srcImgTemp);
        sideDatumIndex++;
#endif
        resptVec.push_back(contours);
    }

    vector<cv::Point2f> datum;
    edgedetect obj;
    obj.GetSideDatum616(resptVec[0], resptVec[1], datum);
    datum.clear();
    datum = { cv::Point2f((156+rangVec2[0][0].x), (161+rangVec2[0][0].y)),
              cv::Point2f((1432+rangVec2[1][0].x), (150+rangVec2[1][0].y)),
              cv::Point2f(-466+rangVec2[1][0].x, 150+rangVec2[1][0].y)
            };
    AlgBase::datumSideVec[id] = datum;
    AlgBase::flagsideVec[id]  = true;
    (*AlgBase::cvarptrsideVec[id]).notify_all();
    // std::cout<<"ok"<<std::endl;
}
